import { createSlice, type PayloadAction } from '@reduxjs/toolkit'
import type { UserInfo } from '../api/api-types'

interface UserState {
  userInfo: Partial<UserInfo> | null;  
}

const initialState: UserState = {
  userInfo: null, 
};

const userSlice = createSlice({
  name: 'userSlise',
  initialState,
  reducers: {
    changeToken: (states, action: PayloadAction<UserInfo>) => {
      states.userInfo = action.payload
    },
    changeUser:(state, action: PayloadAction<{username: string}>) => {
       if (state.userInfo) {
        state.userInfo.username = action.payload.username;
      }
    },
    changeAvatar: (state, action: PayloadAction<{avatar: string}>) => {
      if (state.userInfo) {
        state.userInfo.avatar = action.payload.avatar
      }
    } 
  }
})



export const { changeToken, changeUser, changeAvatar } = userSlice.actions
export default userSlice.reducer